package com.yj.lottery_system.dao.mapper;

import com.yj.lottery_system.dao.dataObject.WinningRecodesDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper

public interface WinningRecordMapper {
    @Insert("<script> " +
            "insert into winning_record  " +
            "(activity_id,activity_name, " +
            "prize_id, prize_name, prize_tier," +
            "winner_id,winner_name,winner_email,winner_phone_number,winning_time) " +
            "values <foreach item = 'item' collection='items' index='index' separator=','  > " +
            " (#{item.activityId},#{item.activityName}," +
            "#{item.prizeId},#{item.prizeName},#{item.prizeTier}," +
            "#{item.winnerId},#{item.winnerName},#{item.winnerEmail,},#{item.winnerPhoneNumber},#{item.winningTime}) " +
            "</foreach>" +
            "</script>")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void batchInsert(@Param("items") List<WinningRecodesDO> winningRecodesDOList);

    /**
     * 根据活动id 查询全部中奖信息
     * @param activityId
     * @return
     */
    @Select("select * from winning_record where activity_id = #{activityId}")
    List<WinningRecodesDO> selectByActivityId(@Param("activityId") Long activityId);

    /**
     * 根据活动id 和 奖品id 查中奖者人数
     * @param activityId
     * @param prizeId
     * @return
     */
    @Select("select count(1) from winning_record where activity_id = #{activityId} and prize_id = #{prizeId}")
    int count(@Param("activityId")Long activityId, @Param("prizeId")Long prizeId);

    /**
     *  根据活动id 或 奖品id 删除中奖记录
     * @param activityId
     * @param prizeId
     */
    @Delete("<script>" +
            "delete from winning_record " +
            "where activity_id = #{activityId} " +
            "<if test=\"prizeId!=null\">" +
            "and prize_id = #{prizeId}" +
            "</if>" +
            "</script>")
    void deleteRecords(@Param("activityId")Long activityId, @Param("prizeId")Long prizeId);
    /**
     *  根据活动id 或 奖品id 查询中奖记录
     * @param activityId
     * @param prizeId
     */
    @Select("<script>" +
            "select * from winning_record " +
            "where activity_id = #{activityId} " +
            "<if test=\"prizeId!=null\">" +
            "and prize_id = #{prizeId}" +
            "</if>" +
            "</script>")
    List<WinningRecodesDO> selectByActivityIdOrPrizeId(@Param("activityId")Long activityId, @Param("prizeId")Long prizeId);

}
